package com.tico.translatereditor.controller;

import com.alibaba.fastjson.JSONObject;
import com.tico.common.pojo.Result;
import com.tico.common.pojo.ResultHelper;
import com.tico.common.utils.BaseQuery;
import com.tico.common.utils.JsonUtils;
import com.tico.common.utils.QueryResult;
import com.tico.translatereditor.domain.Kafka;
import com.tico.translatereditor.param.KafkaParam;
import com.tico.translatereditor.service.KafkaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@RequestMapping("/kafka")
@Slf4j
public class KafkaController {
    @Autowired
    private KafkaService kafkaService;

    @PostMapping(value = "/save")
    public JSONObject test(@RequestBody Kafka kafka, HttpServletRequest request) {
        Result res = new Result();
        try {
            kafka.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            kafka.setStatus("false");
            kafkaService.insert(kafka);
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
    @GetMapping(value = "/list")
    public JSONObject list(KafkaParam kafka) {
        Result result = new Result();
        try {
            BaseQuery baseQuery = new BaseQuery();
            baseQuery.setPage(kafka.getPage());
            baseQuery.setLimit(kafka.getLimit());
            QueryResult<Kafka> pageInfo = kafkaService.findPageInfo(baseQuery);
            result.addModel("count",pageInfo.getTotalRecord());
            result.addModel("data", JsonUtils.toUnderlineJSONFilterString(Kafka.class,pageInfo.getResultList(),new String[]{}) );
            result.setMessage("成功");
            result.setSuccess(true);
        }catch (Exception e){
            result.setMessage("失败");
            result.setSuccess(false);
        }
        return ResultHelper.renderAsJsonWipeData(result);
    }
    @PostMapping(value = "/delete")
    public JSONObject delete(@RequestBody KafkaParam kafkaParam, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = kafkaParam.getIds().split(",");
            for(int i=0;i<str.length;i++){
                kafkaService.delete(str[i]);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }

    @PostMapping(value = "/startUsing")
    public JSONObject startUsing(@RequestBody Kafka kafka, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = kafka.getIds().split(",");
            for(int i=0;i<str.length;i++){
                kafka.setId(str[i]);
                kafkaService.update(kafka);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
}
